package top.somliy.platform.util;

import org.springframework.web.socket.WebSocketSession;
import top.somliy.platform.constants.PlatformConstants;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 类名： @ClassName UrlUtil
 * 创建人：@author zhao dong
 * 类描述：@Description:
 * 创建时间: 2023/11/12 20:15
 */
public final class UrlUtil {
    private UrlUtil() {
    }

    public static Map<String, String> getParamsFromURI(URI uri) {
        Map<String, String> params = new HashMap<>(PlatformConstants.INT_2);
        String query = uri.getQuery();
        if (query != null) {
            String[] keyValuePairs = query.split("&");
            for (String keyValuePair : keyValuePairs) {
                String[] keyValue = keyValuePair.split("=");
                if (keyValue.length == 2) {
                    String key = keyValue[0];
                    String value = keyValue[1];
                    params.put(key, value);
                }
            }
        }
        return params;
    }

    /**
     * 获取请求参数
     *
     * @param session session
     * @return 参数
     */
    public static String getUriParamKey(WebSocketSession session) {
        URI uri = session.getUri();
        Map<String, String> paramsFrom = getParamsFromURI(Objects.requireNonNull(uri));
        return paramsFrom.get(PlatformConstants.STR_KEY);
    }

    /**
     * 获取请求参数
     *
     * @param session session
     * @return 参数
     */
    public static String getUriPath(WebSocketSession session) {
        URI uri = session.getUri();
        if (uri == null) {
            throw new RuntimeException("连接地址为空");
        }
        return getUriPathKey(uri);
    }

    /**
     * 获取请求参数
     *
     * @param uri uri
     * @return 参数
     */
    public static String getUriPathKey(URI uri) {
        String path = uri.getPath();
        return path.substring(path.lastIndexOf('/') + 1);
    }
}
